﻿using Liuliu.ScriptEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GmControlTools
{
    /// <summary>
    /// 发送公告
    /// </summary>
    internal class SendNotice
    {
        /// <summary>
        /// 根据玩家名字下线玩家
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static bool SendNoticeMessage(string Content,int NoticeType,string SendDate)
        {
            DmPlugin dm = Program.dm;
            int pid = Program.process.Id;
            int Hwnd2 = (int)Program.process.MainWindowHandle;
            int Hwnd = dm.FindWindow("", $"Gm{pid}");
            dm.SetWindowSize(Hwnd, 1024, 768);
            dm.BindWindowEx(Hwnd, "gdi", "windows3", "windows", "", 0);//绑定窗体
            var dm_ret = dm.MoveToClick(211, 12);
            if (dm_ret)
            {
                Thread.Sleep(20);
                int menuWindow = FindWindowBySize(Hwnd, 155, 96);
                while (true)
                {
                    if (menuWindow > 0)
                    {
                        //重新绑定窗口
                        dm.UnBindWindow();
                        dm.BindWindowEx(menuWindow, "gdi", "windows3", "windows", "", 0);
                        break;
                    }
                    else
                    {
                        Console.WriteLine($"未找到菜单：公告{Content}");
                        //恢复并激活窗口
                        dm.SetWindowState(Hwnd, 12);
                        Thread.Sleep(1000);
                        //dm.SetWindowState(Hwnd, 3);
                        //Thread.Sleep(50);
                        menuWindow = FindWindowBySize(Hwnd, 155, 96);//继续查找
                        //return false;
                    }
                }
                
                //窗口绑定成功后点击菜单：发送公告
                dm_ret = dm.MoveToClick(60, 37);
                if (dm_ret) {
                    Thread.Sleep(20);
                    int index = 0;
                    while (index <= 4)//最多找4次
                    {
                        int noticeWindow = dm.FindWindow("#32770", "发送公告");
                        if (noticeWindow > 0)
                        {
                            dm.UnBindWindow();
                            dm_ret = dm.BindWindowEx(noticeWindow, "gdi", "windows3", "windows", "", 0);
                            if (dm_ret)
                            {
                                Thread.Sleep(20);
                                int contentHwnds = dm.FindWindowEx(noticeWindow, "Edit", "");
                                dm.SendString(contentHwnds, Content);
                                Thread.Sleep(20);
                                //模拟点击"发送"
                                dm.MoveToClick(512, 604);
                                Thread.Sleep(100);//等待操作响应
                                int msgWindow = dm.FindWindow("#32770", "gm_client");
                                if (msgWindow > 0)
                                {
                                    //有提示消息时，需要关闭消息弹出框
                                    dm.UnBindWindow();
                                    dm_ret = dm.BindWindowEx(msgWindow, "gdi", "windows3", "windows", "", 0);
                                    if (dm_ret)
                                    {
                                        //模拟点击“确定”
                                        dm.MoveTo(100, 110);
                                        dm.LeftClick();
                                        dm.UnBindWindow();
                                    }
                                    else
                                    {
                                        Console.WriteLine("发送公告失败");
                                    }
                                }
                                else
                                {
                                    //发送成功，关闭发送公告接口
                                    Console.WriteLine($"发送公告：{Content}----{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                                    Thread.Sleep(10);
                                    dm.KeyPress(27);
                                    Thread.Sleep(20);
                                    dm.UnBindWindow();
                                    return true;
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine($"未找到发送窗口：公告{Content}");
                        }
                        index++;
                    }
                }
            }
            else
            {
                Console.WriteLine("打开“发送公告”窗口失败");
            }
            return false;
        }

        /// <summary>
        /// 查找指定进程所有可见窗口符合窗口尺寸的窗口句柄
        /// </summary>
        /// <param name="Hwnd">父级窗口句柄</param>
        /// <param name="width">待获取的窗口宽度</param>
        /// <param name="height">待获取的窗口高度</param>
        public static int FindWindowBySize(int Hwnd, int width, int height)
        {
            int result = 0;
            string childHwnds = Program.dm.EnumWindowByProcess(Program.processName, "", "",16);
            if (!string.IsNullOrEmpty(childHwnds))
            {
                string[] hwndArr = childHwnds.Split(',');
                if (hwndArr.Length > 0)
                {
                    foreach (string hwnd in hwndArr)
                    {
                        int x1, y1;
                        Program.dm.GetClientSize(Convert.ToInt32(hwnd), out x1, out y1);

                        if (width == x1 && height == y1)
                        {
                            return Convert.ToInt32(hwnd);
                        }
                    }
                }
            }
            else {
                Program.dm.SetWindowState(Hwnd, 4);
                result = FindWindowBySize(Hwnd, 155, 96);
                Program.dm.SetWindowState(Hwnd, 3);
            }
            return result;
        }
    }
}
